package com.sunriz.motordrive.utils;

import org.junit.Test;

/**
 * ClassName: ArrayUtils
 * Package: com.sunriz.motordrive.utils
 * Description:
 *
 * @Author BuTang
 * @Create 2024/5/13 11:38
 * @Version 1.0
 */
public class ArrayUtils {
    public static byte[] bytesAddValue(byte[] bytes , byte index , byte value){
        byte[] newArray = new byte[bytes.length + 1];
        for (int i = 0, j = 0; i < newArray.length; i++) {
            if (i == index) {
                // 在指定位置添加新的值
                newArray[i] = value;
            } else {
                // 复制原数组的值
                newArray[i] = bytes[j];
                // 移动到原数组的下一个位置
                j++;
            }
        }
        return newArray;
    }


    public static byte[] bytesAddValues(byte[] bytes ,  byte[] value){
        byte[] newArray = new byte[bytes.length + value.length];
        for (int i = 0, j = 0; i < newArray.length; i++) {
            if (i >= bytes.length) {
                // 在指定位置添加新的值
                newArray[i] = value[j];
                j++;
            }else {
                newArray[i] = bytes[i];
            }
        }
        return newArray;
    }


    public static byte[] fillIn0304Msg(int slaveId ,int model ,  int address ,int number){

        byte[] newBytes = new byte[6];
        newBytes[0] = (byte)slaveId;
        newBytes[1] = (byte) model;

        byte[] bytes1 = BaseConversionUtils.intToBytes(address);
        byte[] bytes2 = BaseConversionUtils.intToBytes(number);

        newBytes[2] = bytes1[0];
        newBytes[3] = bytes1[1];
        newBytes[4] = bytes2[0];
        newBytes[5] = bytes2[1];

        return newBytes ;
    }




}
